#define _CRT_SECURE_NO_WARNINGS

#include "stack.h"

void STInit(stack* pt)
{
	assert(pt);
	pt->a = NULL;
	pt->capacity = 0;
	pt->top = 0;
}

void STPush(stack* pt, STDataType x)
{
	assert(pt);
	if (pt->top == pt->capacity)
	{
		int newcapacity = pt->capacity == 0 ? 4 : pt->capacity * 2;
		STDataType* tmp = realloc(pt->a, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}
		pt->a = tmp;
		pt->capacity = newcapacity;
	}
	pt->a[pt->top++] = x;
}
void STPop(stack* pt)
{
	assert(pt && !STEmpty(pt));
	pt->top--;
}

bool STEmpty(stack* pt)
{
	assert(pt);

	return pt->top == 0;
}
STDataType STTop(stack* pt)
{
	assert(pt && !STEmpty(pt));
	return pt->a[pt->top - 1];
}

int STSize(stack* pt)
{
	assert(pt);
	return pt->top;
}

void STDestroy(stack* pt)
{
	assert(pt);

	free(pt->a);
	pt->capacity = 0;
	pt->top = 0;
}



